<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['teacher:evaluation:scheme:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['teacher:evaluation:scheme:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['teacher:evaluation:scheme:remove']">删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="schemeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="方案编号" align="center" prop="id" />
      <el-table-column label="方案名称" align="center" prop="tescName" :show-overflow-tooltip="true" />
      <el-table-column label="方案类别" align="center" prop="tescType" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ tescTypeOptions.filter(function (item, index, arr) {
          return item.dictValue == scope.row.tescType
          })[0].dictLabel
          }}
        </template>
      </el-table-column>
      <el-table-column label="方案状态" align="center" prop="tescStatus" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{
          tescStatusOptions.filter(function (item, index, arr) {
          return item.dictValue == scope.row.tescStatus
          })[0].dictLabel
          }}
        </template>
      </el-table-column>
      <el-table-column label="考核结果类型" align="center" prop="tescResultType" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{(scope.row.tescResultType ==0?'分数制':'等级制')}}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="light" content="修改基本信息" placement="top">
            <el-button size="mini" circle type="success" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['teacher:evaluation:scheme:edit']" />
          </el-tooltip>

          <el-tooltip class="item" effect="light" content="考评指标设置" placement="top">
            <el-button size="mini" circle type="primary" icon="el-icon-setting"
              v-hasPermi="['teacher:evaluation:scheme:setting']" @click="handleSetting(scope.row)" />
          </el-tooltip>
          
          <el-tooltip v-if="scope.row.tescResultType==1" class="item" effect="light" content="考核等级设置" placement="top">
            <el-button size="mini" circle type="primary" icon="el-icon-coin"
              v-hasPermi="['teacher:evaluation:scheme:setting']" @click="handleGread(scope.row)" />
          </el-tooltip>

          <el-tooltip class="item" effect="light" content="删除方案" placement="top">
            <el-button size="mini" circle type="danger" icon="el-icon-delete"
              v-hasPermi="['teacher:evaluation:scheme:remove']" @click="handleDelete(scope.row)" />
          </el-tooltip>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="方案名称" prop="tescName">
          <el-input type="input" v-model="form.tescName" placeholder="请输入方案名称" />
        </el-form-item>
        <el-form-item label="方案类型" prop="tescType">
          <el-select v-model="form.tescType" :aria-required="true" placeholder="请选择方案类型">
            <el-option v-for="dict in tescTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="方案状态" prop="tescStatus">
          <el-select v-model="form.tescStatus" placeholder="请选择方案状态">
            <el-option v-for="dict in tescStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item  label="方案结果类型" prop="tescResultType">
          <el-select v-model="form.tescResultType" placeholder="请选择结果类型">
            <el-option value="0" key="0" label="分数制">分数制</el-option>
            <el-option value="1" key="1" label="等级制">等级制</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <table>
          
        </table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 考评配置对话框 -->
    <el-dialog :title="title" :visible.sync="openItemSetting" width="1000px" :close-on-click-modal="false">
      <el-table :v-loading="loadingItem" :data="tableDataItem" style="width: 100%;margin-bottom: 20px;" row-key="id"
        border default-expand-all :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
        <el-table-column prop="teitContent" label="考评项描述" width="500px">
        </el-table-column>
        <el-table-column prop="tesiPercent" label="打分百分比" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.teitParentId != null" style="margin-left:42px;"></span>
            <el-button size="mini" circle type="success" icon="el-icon-edit"
              @click="handleUpdateEvaItem(scope.row, scope.row.id)" v-hasPermi="['teacher:evaluation:scheme:edit']" />
            <el-button size="mini" circle type="danger" icon="el-icon-delete"
              v-hasPermi="['teacher:evaluation:scheme:remove']" @click="handleRemove(scope.row)" />
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button size="medium" type="primary" style="border:1px solid #1890FF;"
          @click="handleUpdateEvaItem(tableDataItem)">添 加</el-button>
        <el-button type="cancel" style="margin-right: 450px;" @click="closeTable">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 修改考评项对话框 -->
    <el-dialog :title="titleItem" :visible.sync="openItemEdit" width="500px">
      <el-form ref="formEvuItem" :model="formEvuItem" :rules="rulesEvuItem" label-width="100px">
        <el-form-item label="考评项描述" prop="teitContent">
          <el-input type="hidden" v-model="formEvuItem.tescId" style="position: absolute;" />
          <el-input type="input" v-model="formEvuItem.teitContent" placeholder="请输入考评项名称" />
        </el-form-item>
        <el-form-item label="打分百分比" prop="tesiPercent">
          <el-input type="input" v-model="formEvuItem.tesiPercent" placeholder="请输入打分百分比" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="formEvuItem.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormItem">确 定</el-button>
        <el-button @click="cancelEvuItem">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加考评项对话框 -->
    <el-dialog title="添加考评项" :visible.sync="openItemAdd" width="500px">
      <el-form ref="formEvuItem" :model="formEvuItem" :rules="rulesEvuItem" label-width="100px">
        <el-form-item label="考评项">
          <el-select v-model="tableDataName" placeholder="请选择">
            <el-option v-for="item in tableDataItem" :key="item.id" :label="item.teitContent" :value="item.id">
            </el-option>
          </el-select>
          <div class="hint">小提示：没有选择考评项时，会添加到最外层</div>
        </el-form-item>
        <el-form-item label="考评描述" prop="teitContent">
          <el-input type="input" v-model="formEvuItem.teitContent" placeholder="请输入考评描述" />
        </el-form-item>
        <el-form-item label="打分百分比" prop="tesiPercent">
          <el-input type="input" v-model="formEvuItem.tesiPercent" placeholder="请输入打分百分比" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="formEvuItem.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- slot-scope="scope" -->
        <el-button type="primary" @click="submitFormItem">确 定</el-button>
        <el-button @click="openItemAdd = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import { list, get, del, add, update } from "@/api/teacher/evaluation/scheme.js";
import { listEvItem, getEvItem, delEvItem, addEvItem, updateEvItem } from "@/api/teacher/evaluation/evlauation_item.js";
import { Footer } from "element-ui";


export default {
  name: "scheme",
  data() {
    
    return {
      options: [],
      //方案id
      tescIds: 0,
      // 遮罩层
      loading: true,
      loadingItem: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 表格数据
      schemeList: [],
      // 弹出层标题
      title: "",
      titleItem: "",
      titleBizName: "考评方案",
      titleBizItemName: "考评项",
      // 是否显示弹出层
      open: false,
      openItemSetting: false,
      openItemEdit: false,
      openItemAdd: false,
      // 方案状态数据字典
      tescStatusOptions: [],
      // 方案类型数据字段
      tescTypeOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        type: undefined,
        status: undefined
      },
      //考评项管理数组
      tableDataItem: [],
      tableDataName: [],
      // 表单参数
      form: {},
      formEvuItem: {},
      // 表单校验
      rules: {
        tescName: [
          { required: true, message: "方案名称不能为空", trigger: "blur" }
        ],
        tescType: [
          { required: true, message: "方案类型不能为空", trigger: "blur" }
        ],
        tescStatus: [
          { required: true, message: "方案状态不能为空", trigger: "blur" }
        ]
      },
      rulesEvuItem: {
        teitContent: [
          { required: true, message: "考评描述不能为空", trigger: "blur" }
        ],
        tesiPercent: [
          { required: true, message: "打分百分比不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getDicts("t_teacher_evaluation_scheme_tesc_type").then(response => {
      this.tescTypeOptions = response.data;
    });
    this.getDicts("t_teacher_evaluation_scheme_tesc_status").then(response => {
      this.tescStatusOptions = response.data;
    });
    this.getList();
  },
  methods: {
    //清空方法
    Empty(){
      this.formEvuItem={
        "id": undefined,
				"teitContent": "",
				"teitParentId": undefined,
				"tescId": undefined,
				"tesiId": undefined,
				"tesiPercent": ""
      }
    },
    /** 表格数据列表 */
    getList() {
      this.loading = true;
      list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.schemeList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      }
      );
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 考评项管理关闭按钮
    closeTable() {
      this.openItemSetting = false;
    },
    // 考评修改取消按钮
    cancelEvuItem() {
      this.openItemEdit = false;
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        type: undefined,
        status: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加" + this.titleBizName;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      get(id).then(response => {
        this.title = "修改" + this.titleBizName;
        this.form = response.data;
        this.open = true;
      });
    },

    /** 考评项设置按钮操作 */
    handleSetting(row) {
      console.log(row, '考评项设置按钮操作');
      this.loadingItem = true;
      this.tescIds = row.id
      listEvItem(this.tescIds).then(response => {
        this.openItemSetting = true;
        this.title = "老师考评项管理";
        this.tableDataItem = response.data;
        this.loadingItem = false;
      });
    },
    handleGread(row) {
      this.$router.push({path:"/teacher/evaluation/scheme/gread", query:{tescId: row.id}})
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            update(this.form.id, this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.message);
              }
            });
          } else {
            add(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.message);
              }
            });
          }
        }
      });
    },
    /** 考评项修改按钮操作 */
    handleUpdateEvaItem(row, rowid) {
      if (rowid != null) {
        console.log(row, '进修改')
        const id = row.id || this.ids;
        getEvItem(id).then(response => {
          this.titleItem = "修改" + this.titleBizItemName;
          this.formEvuItem = response.data;
          this.openItemEdit = true;
        });
      } else {
        this.openItemAdd = true;
        this.Empty();
      }
    },
    /** 提交按钮 */
    submitFormItem: function () {
      this.$refs["formEvuItem"].validate(valid => {
        if (valid) {
          console.log(this.formEvuItem,'添加');
          if (this.formEvuItem.id !== undefined) {
            console.log(this.formEvuItem.id,'修改考评项id')
            updateEvItem(this.formEvuItem.id, this.formEvuItem).then(response => {
              console.log(response, '修改考评项')
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.openItemEdit = false;
                this.refreshSettingTable(this.formEvuItem.tescId);
              } else {
                this.msgError(response.message);
              }
            });
          } else {
            var list = {
              tescId: this.tescIds,
              teitContent: this.formEvuItem.teitContent,
              tesiPercent: this.formEvuItem.tesiPercent,
              remarks: this.formEvuItem.remarks,
              teitParentId: this.tableDataName
            }
            addEvItem(list).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.openItemAdd = false;
                this.refreshSettingTable(this.tescIds);
              } else {
                this.msgError(response.message);
              }
            });
          }
        }
      })
    },
    refreshSettingTable(tiscId) {
      this.loadingItem = true;
      listEvItem(tiscId).then(response => {
        console.log(response, '查看列表')
        this.tableDataItem = response.data;
        this.loadingItem = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row, '删除方案')
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除方案编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return del(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(function () { });
    },
    //删除考评项
    handleRemove(row) {
      console.log(row, '删除考评项');
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该考评项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delEvItem(ids);
      }).then(() => {
        this.refreshSettingTable(this.tescIds)
        this.msgSuccess("删除成功");
      }).catch(function () { });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportType(queryParams);
      }).then(response => {
        this.fileDownload(response.data, "学年学期.xlsx");
      }).catch(function () { });
    }

  }
};
</script>
<style>
.hint {
  font-size: 10px;
  height: 10px;
  color: rgb(253, 93, 93);
}
</style>
